package com.xiaol.springbootredis.web;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xiaol.springbootredis.mapper.db1.User1Mapper;
import com.xiaol.springbootredis.mapper.db2.User2Mapper;
import com.xiaol.springbootredis.model.Person;
import com.xiaol.springbootredis.model.domain.User;
import com.xiaol.springbootredis.model.query.UserQuery;
import com.xiaol.springbootredis.redis.RedisOptionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * User: Ray.Shaw <p />
 * Time: 2018/7/27 16:17  <p />
 * Version: V1.0  <p />
 * Description:  <p />
 */
@RestController
@RequestMapping
public class TestController {
    @Autowired
    private User1Mapper user1Mapper;

    @Autowired
    private User2Mapper user2Mapper;

    @Autowired
    private RedisOptionService redisOptionService;


    @RequestMapping("/getUsers")
    public PageInfo<User> getUsers(UserQuery query) {
        PageHelper.startPage(query.getPageNum(), query.getPageSize());
        List<User> users=user1Mapper.getAll();
        PageInfo<User> pageInfo = new PageInfo<>(users);
        return pageInfo;
    }

    @RequestMapping("/getUser")
    public User getUser(Long id) {
        User user=user2Mapper.getOne(id);
        return user;
    }

    /**
     * 演示Redis工具类调用
     *
     * @Title: setString
     * @return
     *
     */
    @RequestMapping("demo")
    public String demo() {
        String key = "springboot_redis";

        // 演示 字符串操作
        String eshengtai = "HelloWorld~";
        System.out.println("-----------------字符串开始------------------");
        redisOptionService.setStr(key, eshengtai);
        System.out.println("-----------------保存完成，获取数据内容------------------");
        System.out.println(redisOptionService.getString(key));
        System.out.println("-----------------字符串结束------------------");

        // 演示 对象操作
        String keyObj = "springboot_redis_obj";
        Person est = new Person();
        est.setId("100");
        est.setName("e生态");

        System.out.println("-----------------对象开始------------------");
        redisOptionService.setObject(keyObj, est);
        System.out.println("-----------------保存完成，获取数据内容------------------");
        Person estCache = (Person) redisOptionService.getObject(keyObj);
        System.out.println(estCache.getId());
        System.out.println(estCache.getName());
        System.out.println("-----------------对象结束------------------");

        // 演示 map操作

        String keyMap = "springboot_redis_map";
        Map map = new HashMap();
        map.put("eshengtai", "eshengtai");
        map.put("eshengtai100", "eshengtaiVal100");
        System.out.println("-----------------Map开始------------------");
        // redisOptionService.setMap(keyMap, map);
        System.out.println("-----------------保存完成，获取数据内容------------------");
        System.out.println(redisOptionService.getMap(keyMap));
        System.out.println("-----------------Map结束------------------");

        // 演示 list操作

        String keylist = "springboot_redis_list";
        List list = new ArrayList();
        list.add(100);
        list.add(2000);
        System.out.println("-----------------List开始------------------");
        // redisOptionService.setList(keylist, list);
        System.out.println("-----------------保存完成，获取数据内容------------------");
        System.out.println(redisOptionService.getList(keylist));
        System.out.println("-----------------List结束------------------");

        return "演示成功~";
    }
}
